package token;

import java.util.concurrent.ConcurrentHashMap;

public class TokenUtils {

    public final static ConcurrentHashMap<String,Token> TOKEN = new ConcurrentHashMap<String,Token>();
    public static String setToken(String token,long expireTime){

        Token t  = new Token();
        t.setTokenName(token);
        //换成毫秒值
        //t.setExpireTime(System.currentTimeMillis());
        t.setExpireTime(expireTime);
        TokenUtils.TOKEN.put("token",t);
        return TokenUtils.TOKEN.get("token").getTokenName();
    }


    public static synchronized String getToken(){

        if (TokenUtils.TOKEN.get("token") == null){
            //调用接口
            return TokenUtils.setToken("12345",70000);
        }
        Long expireTime = TokenUtils.TOKEN.get("token").getExpireTime();
        long time = System.currentTimeMillis() - expireTime;
        if (time >= 7200*1000){
            //重修获取接口
           return TokenUtils.setToken("124444",17888);
        }
        return TokenUtils.TOKEN.get("token").getTokenName();
    }
}
